const api = require('../../utils/request.js')
const CONFIG = require('../../config.js')
//获取应用实例
var app = getApp();
Page({
  data: {
    remind: '加载中',
    angle: 0,
    userInfo: {}
  },
  goToIndex:function(e){    
    // api.fetchRequest('/template-msg/wxa/formId', {
    //   token: wx.getStorageSync('token'),
    //   type: 'form',
    //   formId: e.detail.formId
    // })
    console.log("点击了进入按钮")
    if (app.globalData.isConnected) {
      console.log("--true----")
      //switchTab  redirectTo
      new Promise((resolve, reject) => {
          wx.request({
            url: CONFIG.subDomain + "weChatAction/autoLogin.do",
            data: {
              "openID": "gh_d17c225d5ec3",
              "userOpenID": app.globalData.userInfo.openId,
            },
            success: (res) => {
              console.log("自动登录")
              if(res.data.success){
                wx.setStorageSync("checkToken", res.data.chcToken);
                console.log("自动登录success -- checkToken" + app.globalData.chcToken)
                wx.switchTab({
                  url: '/pages/check/check'
                })
              }else{
                console.log("自动登录false")
                wx.redirectTo({
                  url: '/pages/login/login',
                });
              }
              
            },
            fail: () =>{
              wx.redirectTo({
                url: '/pages/login/login',
              });
            }
          })

        }).catch(() => {
          wx.redirectTo({
            url: '/pages/login/login',
          });
        })
     
    } else {
      console.log("---false----")
      wx.showToast({
        title: '当前无网络',
        icon: 'none',
      })
    }
  },
  onLoad:function(){
    var that = this
    wx.setNavigationBarTitle({
      title: wx.getStorageSync('mallName')
    })
  },
  onShow:function(){
    let that = this
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      app.goLoginPageTimeOut()
    } else {
      that.setData({
        userInfo: userInfo
      })
    }
  },
  onReady: function(){
    var that = this;
    setTimeout(function(){
      that.setData({
        remind: ''
      });
    }, 1000);
    wx.onAccelerometerChange(function(res) {
      var angle = -(res.x*30).toFixed(1);
      if(angle>14){ angle=14; }
      else if(angle<-14){ angle=-14; }
      if(that.data.angle !== angle){
        that.setData({
          angle: angle
        });
      }
    });
  }
});